<?php
/**
 * Object Relational Persistence Mapping Class for table: medical_material
 *
 * @package	com.athena
 * @author	LHV
 */
class MedicalMaterial extends ORDataObject {

	/**#@+
	 * Fields of table: medical_material mapped to class members
	 */
	var $medical_material_id	= '';
	var $vi_name		            = '';
	var $en_name                  = '';
	var $unit_id		        = '';
	var $price		            = '';
	var $insurance_price        = '';
	var $enum_material_type		= '';
	var $note		            = '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'medical_material';

	/**
	 * Primary Key
	 */
	var $_key = 'medical_material_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'MedicalMaterial';

	/**
	 * Handle instantiation
	 */
	function MedicalMaterial() {
		parent::ORDataObject();
	}

	public function getPrice(){
		return $this->price;
	}
	/**#@-*/

    /**
     * Get information on a medical material item.
     *
     * @param string $materialName Material name
     * @return array Result array
     */
	public function checkForExistingMaterialName($materialName){
	    $field      =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
        $insuranceItemType  =   Utils::getEnumKey(EnumerationConstant::SER_INS_ITEM_TYPE, EnumerationConstant::SER_INS_ITEM_TYPE_MAT);
        $ds = new Datasource_sql();
        $sql = array(
            'cols'      => "t.medical_material_id, t.$field material_name, eu.$field unit,
                    t.price, t.insurance_price, si.insurance_support_id",
            'from'      => "$this->_table t
                            LEFT JOIN equipment_unit eu ON(t.unit_id = eu.equipment_unit_id)
                            LEFT JOIN $l10Table l1 ON t.name=l1.$l10Key
                            LEFT JOIN $l10Table l2 ON eu.name=l2.$l10Key
                            LEFT JOIN service_insurance_map si
                                ON (t.medical_material_id=si.item_id AND si.enum_item_type=$insuranceItemType)",
            'where'     => "l1.$field = '$materialName'"
            );

        $ds->setup($this->_db,$sql);
        $result = $ds->toArray();
        if (empty($result)) return false;
        return $result[0];
    }

    /**
     * Get material name suggest with a provided name part.
     *
     * @param string $namePart Part of material name
     * @return array Result array
     */
    function getMaterialSuggestionNames($namePart){
        $field  =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');

        $sql = "SELECT t.$field as name
                FROM $this->_table t
                WHERE t.$field LIKE '$namePart%'
                LIMIT 20";
                
        $rows = $this->_db->GetAll($sql);
        $result = array();
        for ($i = 0; $i < sizeof($rows); $i++){
            $result[$i] = $rows[$i]['name'];
        }
        return $result;
    }

}
?>
